'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _antd = require('antd');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

/**
 * 数据字典复选框
 * @author: ljq
 * @since: 2018-10-26 11:55:09
 */
var CheckboxGroup = _antd.Checkbox.Group;

var DictCheckboxGroup = function (_React$Component) {
    (0, _inherits3['default'])(DictCheckboxGroup, _React$Component);

    function DictCheckboxGroup(props) {
        (0, _classCallCheck3['default'])(this, DictCheckboxGroup);

        var _this = (0, _possibleConstructorReturn3['default'])(this, (DictCheckboxGroup.__proto__ || Object.getPrototypeOf(DictCheckboxGroup)).call(this, props));

        _this.getCheckboxGroup = function () {
            var _this$props = _this.props,
                dictkey = _this$props.dictkey,
                filterkeys = _this$props.filterkeys,
                order = _this$props.order,
                storagetype = _this$props.storagetype;

            var items = void 0;
            if (storagetype && storagetype === "localStorage") {
                items = localStorage['dict_' + dictkey];
            } else {
                items = sessionStorage['dict_' + dictkey];
            }
            if (!items) {
                return '';
            }
            items = JSON.parse(items);
            if (filterkeys) {
                items = items.filter(function (item) {
                    return filterkeys.includes(item.itemKey);
                });
            }
            if (order) {
                var orderItems = order.map(function (itemKey) {
                    return items.filter(function (item) {
                        return item.itemKey === itemKey;
                    })[0];
                });
                orderItems.push.apply(orderItems, (0, _toConsumableArray3['default'])(items.filter(function (item) {
                    return !order.includes(item.itemKey);
                })));
                items = orderItems;
            }
            var options = items.map(function (item) {
                return {
                    label: item.itemName,
                    value: item.itemKey
                };
            });
            return React.createElement(CheckboxGroup, (0, _extends3['default'])({}, _this.props, { options: options, className: 'dict-checkbox-group' }));
        };
        // 校验
        var dictkey = _this.props.dictkey;

        if (!dictkey) {
            throw new Error('Dict.CheckboxGroup组件需要属性：dictkey');
        }
        return _this;
    }

    (0, _createClass3['default'])(DictCheckboxGroup, [{
        key: 'render',
        value: function render() {
            return this.getCheckboxGroup();
        }
    }]);
    return DictCheckboxGroup;
}(React.Component);

exports['default'] = DictCheckboxGroup;
module.exports = exports['default'];